require(['./config'], () => {
  require(['template', 'url', 'header', 'footer'], (template, url) => {
    class Shop {
      constructor() {
        this.showPadData()
        this.showWindowData()
        this.showAndroidData()
        this.showOtherData()
        this.showCartTotalNum ()
      }
      // 渲染电脑模块
      showPadData() {
        new Promise(resolve => {
          // http://rap2api.taobao.org/app/mock/230229/pad/get
          $.get(`${url.rapBaseUrl}/pad/get`, resp => {
            resolve(resp)
          })
        }).then(resp => {
          console.log(resp)
          if (resp.code === 200) {
            let str = template('pad-template', { ...resp.body.info })
            $('#pad').html(str)
          }
        })
      }
      //渲染window模块
      showWindowData() {
        new Promise(resolve => {
          // http://rap2api.taobao.org/app/mock/230229/pad/get
          $.get(`${url.rapBaseUrl}/window/get`, resp => {
            resolve(resp)
          })
        }).then(resp => {
          console.log(resp)
          if (resp.code === 200) {
            let str = template('window-template', { ...resp.body.info })
            $('#window').html(str)
          }
        })
      }
      //渲染Android模块
          showAndroidData() {
    new Promise(resolve => {
          // http://rap2api.taobao.org/app/mock/230229/pad/get
          $.get(`${url.rapBaseUrl}/Android/get`, resp => {
            resolve(resp)
          })
        }).then(resp => {
          console.log(resp)
          if (resp.code === 200) {
            let str = template('android-template', { ...resp.body.info })
            $('#android').html(str)
          }
        })
      }
      //渲染配件模块
      showOtherData() {
        new Promise(resolve => {
          // http://rap2api.taobao.org/app/mock/230229/pad/get
          $.get(`${url.rapBaseUrl}/other/get`, resp => {
            resolve(resp)
          })
        }).then(resp => {
          console.log(resp)
          if (resp.code === 200) {
            let str = template('other-template', { ...resp.body.info })
            $('#other').html(str)
          }
        })
      }
      //显示购物车数量
      //(需要处理其他页面上数据添加后此处的数量更新)
      showCartTotalNum () {
        // 把localStorage取出来得到总数量
        let cart = localStorage.getItem('cart')
        let num = 0;
        if (cart) {
          cart = JSON.parse(cart)
          // 计算cart的总数量
          num = cart.reduce((res, shop) => {
            res += shop.num
            return res
          }, 0)
          $('#shopNum').html(num)
        }
      }

    }

    new Shop()
  })
})